package cn.demomaster.quickdb_sample.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.demomaster.quickdb.R;
import cn.demomaster.quickdb.model.ColumnInfo;

public class TableAdapter extends cn.demomaster.quickdb.adapter.TableAdapter {

    public TableAdapter(Context context) {
        super(context);
    }

    @Override
    public ViewGroup getRowLayout(int rowIndex) {
        if (rowIndex == -1) {
            ViewGroup view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.table_layout_row, null, false);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return view;
        }

        ViewGroup view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.table_layout_row, null, false);
        view.setTag(rowIndex-1);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int rowIndex = (int) v.getTag();
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, rowIndex);
                }
                if(!v.isSelected()) {
                    v.setSelected(true);
                    v.setBackground(context.getResources().getDrawable(cn.demomaster.quickdb_sample.R.drawable.layout_selector));
                }
            }
        });
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        //view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return view;
    }

    @Override
    public View getCellView(ViewGroup patentView, int viewType) {
        View view = null;
        if (viewType == -1) {//header
            view = LayoutInflater.from(context).inflate(R.layout.table_layout_cell, null, false);
        } else {//body
            view = LayoutInflater.from(context).inflate(R.layout.table_layout_cell, null, false);
        }
        int w = 300;
        if (data != null && !data.isEmpty() && data.get(0) != null && data.get(0).length > 0) {
            int w1 = getScreenWidth(context) / data.get(0).length;
            w = Math.max(w, w1);
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(w, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        view.setLayoutParams(layoutParams);
        return view;
    }

    private int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public void bindRowViewData(int rowIndex, ViewGroup rowlayout) {
        if (rowIndex == -1) {
            rowlayout.setBackgroundColor(Color.BLUE);
        } else {
            rowlayout.setBackgroundColor(rowIndex % 2 == 0 ? Color.GRAY : Color.WHITE);
        }
    }

    @Override
    public void bindCellViewData(int rowIndex, int columnIndex, ViewGroup viewGroup, View itemView) {
        View view = itemView.findViewById(R.id.tv_text);
        if(view instanceof TextView) {
            if (rowIndex == -1) {
                ((TextView)view).setTextColor(Color.WHITE);
            } else {
                ((TextView)view).setText(data.get(rowIndex)[columnIndex]);
            }
        }
    }

    @Override
    public String formatColumnValue(String tableName, ColumnInfo columnInfo, Object value) {
        if(columnInfo.getColumnName().equals("age")){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(new Date(Long.valueOf(value+"")));
        }else if(columnInfo.getColumnName().equals("sex")){
            return String.format(Integer.valueOf(value+"")==1?"男":"女");
        }
        return null;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}